package com.web.news.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.web.news.pojo.Comments;
import com.web.news.pojo.News;
import com.web.news.pojo.NewsDo;
import com.web.news.service.NewsService;
import com.web.news.service.impl.NewsServiceImpl;


public class NewsServlet extends HttpServlet {
		
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		response.setContentType("text/html;charset=UTF-8");
		request.setCharacterEncoding("UTF-8");
		
		String action = request.getParameter("action");
		
		if("listNews".equals(action)){
			listNews(request,response);
		} else if ("detail".equals(action)){
			newsDetail(request,response);
		} else if ("doComment".equals(action)){
			doComment(request,response);
		}
		

	}

	private void doComment(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		String cauthor = request.getParameter("cauthor");
		String cip = request.getParameter("cip");
		String ccontent = request.getParameter("ccontent");
		int cnid = Integer.parseInt(request.getParameter("cnid"));
		
		NewsService newsService = new NewsServiceImpl();
		
		Comments comments = new Comments();
		comments.setCnid(cnid);
		comments.setCauthor(cauthor);
		comments.setCip(cip);
		comments.setCcontent(ccontent);
		
		newsService.addComments(comments);
		
		response.sendRedirect(request.getContextPath()+"/NewsServlet?action=detail&nid="+cnid);
		
	}

	private void newsDetail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		int nid = Integer.parseInt(request.getParameter("nid"));
		NewsService newsService = new NewsServiceImpl();
		
		NewsDo newsDo = newsService.findNewDetailById(nid);
		List<Comments> commentList =  newsService.findCommentsByNews(nid);
		
		String ip = request.getRemoteAddr();
		
		
		request.setAttribute("newsDetail", newsDo);
		request.setAttribute("commentList", commentList);
		request.setAttribute("cip", ip);
		
		request.getRequestDispatcher("/news_read.jsp").forward(request,response);
	}

	private void listNews(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		NewsService newsService = new NewsServiceImpl();
		int pageNum = 1;
		if(request.getParameter("pageNum")!= null ){
			pageNum = Integer.parseInt(request.getParameter("pageNum"));
		}
		int pageSize = 10;
		PageHelper.startPage(pageNum,pageSize);
		List<News> list = newsService.findAllNews();
		
		PageInfo page = new PageInfo<>(list);
		
		request.setAttribute("newsList", list);
		request.setAttribute("page", page);
		
		request.getRequestDispatcher("/index.jsp").forward(request, response);
	}

}
